package com.xmb.wechat.widget;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.common.Constant;

/**
 * @Author lijiangop
 * @CreateTime 2020/4/11 14:06
 */
public class TitleLayout extends RelativeLayout {

    private TextView mTvTitle, mTvTitleLeft, mTvTitleRight;
    private ImageView mIvMore, mIvBack, mIvMute;
    private View mView, mPlaceHolder;

    public TitleLayout(Context context) {
        this(context, null);
    }

    public TitleLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleLayout);
        initView(context, typedArray);
        typedArray.recycle();
    }

    private void initView(Context context, TypedArray typedArray) {
        int color = typedArray.getColor(R.styleable.TitleLayout_bg, getResources().getColor(R.color.common_title_bar_bg));
        String title = typedArray.getString(R.styleable.TitleLayout_title);
        boolean titleBlod = typedArray.getBoolean(R.styleable.TitleLayout_title_blod, false);

        String leftTitle = typedArray.getString(R.styleable.TitleLayout_left_title);
        boolean leftTitleBlod = typedArray.getBoolean(R.styleable.TitleLayout_left_title_blod, false);

        String rightTitle = typedArray.getString(R.styleable.TitleLayout_right_title);
        boolean rightTitleBlod = typedArray.getBoolean(R.styleable.TitleLayout_right_title_blod, false);

        int ivLeft = typedArray.getResourceId(R.styleable.TitleLayout_left_image, R.drawable.ic_wx_back_black1380);
        int ivRight = typedArray.getResourceId(R.styleable.TitleLayout_right_image, 0);
        //findView
        mView = LayoutInflater.from(context).inflate(R.layout.layout_title, null, false);
        mPlaceHolder = mView.findViewById(R.id.view_placeholder);
        mIvBack = mView.findViewById(R.id.iv_back);
        mIvMore = mView.findViewById(R.id.iv_more);
        mTvTitle = mView.findViewById(R.id.tv_title);
        mTvTitleLeft = mView.findViewById(R.id.tv_title_left);
        mTvTitleRight = mView.findViewById(R.id.tv_title_right);
        mIvMute = mView.findViewById(R.id.iv_mute);
        //init
        mView.setBackgroundColor(color);
        if (!TextUtils.isEmpty(title))
            mTvTitle.setText(title);
        if (titleBlod)
            mTvTitle.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        if (!TextUtils.isEmpty(leftTitle))
            mTvTitleLeft.setText(leftTitle);
        if (leftTitleBlod)
            mTvTitleLeft.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        if (!TextUtils.isEmpty(rightTitle))
            mTvTitleRight.setText(rightTitle);
        if (rightTitleBlod)
            mTvTitleRight.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        mIvBack.setImageResource(ivLeft);
        if (ivRight != 0)
            mIvMore.setImageResource(ivRight);
        //事件
        mPlaceHolder.setOnClickListener(v -> {
            if (context instanceof Activity) {
                Activity activity = (Activity) context;
                activity.onBackPressed();
            }
        });
        addView(mView, new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    }

    /**
     * 调整View大小
     */
    public void adjustViewSize() {
        int fontScaleLevel = SPUtils.getInstance().getInt(Constant.SP.SP_KEY_FONT_SCALE_LEVEL, Constant.SP.SP_DEFAULT_FONT_SCALE_LEVEL);
        //textView
        if (fontScaleLevel < Constant.mTitleLayoutTextScale.length) {
            mTvTitle.setTextSize(Constant.mTitleLayoutTextScale[fontScaleLevel] * 17);
            mTvTitleLeft.setTextSize(Constant.mTitleLayoutTextScale[fontScaleLevel] * 17);
            mTvTitleRight.setTextSize(Constant.mTitleLayoutTextScale[fontScaleLevel] * 17);
        }
        int height = SizeUtils.dp2px(50f);
        if (fontScaleLevel < Constant.mTitleLayoutCommonScale.length) {
            //viewHolder
            int placeHolderResult = Float.valueOf(SizeUtils.dp2px(40f) * Constant.mTitleLayoutCommonScale[fontScaleLevel]).intValue();
            ViewGroup.LayoutParams placeHolderLayoutParams = mPlaceHolder.getLayoutParams();
            placeHolderLayoutParams.width = placeHolderResult;
            mPlaceHolder.setLayoutParams(placeHolderLayoutParams);
            //ivBack
            int backResult = Float.valueOf(SizeUtils.dp2px(18f) * Constant.mTitleLayoutCommonScale[fontScaleLevel]).intValue();
            ViewGroup.LayoutParams backLayoutParams = mIvBack.getLayoutParams();
            backLayoutParams.width = backResult;
            backLayoutParams.height = backResult;
            mIvBack.setLayoutParams(backLayoutParams);
            //ivMute
            int muteResult = Float.valueOf(SizeUtils.dp2px(15f) * Constant.mTitleLayoutCommonScale[fontScaleLevel]).intValue();
            ViewGroup.LayoutParams muteLayoutParams = mIvMute.getLayoutParams();
            muteLayoutParams.width = muteResult;
            muteLayoutParams.height = muteResult;
            mIvMute.setLayoutParams(muteLayoutParams);
            //ivMore
            int moresResult = Float.valueOf(SizeUtils.dp2px(17f) * Constant.mTitleLayoutCommonScale[fontScaleLevel]).intValue();
            ViewGroup.LayoutParams moreLayoutParams = mIvMore.getLayoutParams();
            moreLayoutParams.width = moresResult;
            moreLayoutParams.height = moresResult;
            mIvMore.setLayoutParams(moreLayoutParams);
            //viewGroup
            height = Float.valueOf(ScreenUtils.getScreenWidth() * 0.12f * Constant.mTitleLayoutCommonScale[fontScaleLevel]).intValue();
        }
        ViewGroup.LayoutParams layoutParams = getLayoutParams();
        layoutParams.height = height;
        setLayoutParams(layoutParams);
    }

    public void setTitle(String value) {
        if (mTvTitle != null)
            mTvTitle.setText(TextUtils.isEmpty(value) ? "" : value);
    }

    public void setTitleLeft(String value) {
        if (mTvTitleLeft != null)
            mTvTitleLeft.setText(TextUtils.isEmpty(value) ? "" : value);
    }

    public void setTitleRight(String value) {
        if (mTvTitleRight != null)
            mTvTitleRight.setText(TextUtils.isEmpty(value) ? "" : value);
    }

    public void setImageRight(int res) {
        if (mIvMore != null)
            mIvMore.setImageResource(res);
    }

    public void setRightClick(OnClickListener onClickListener) {
        if (mTvTitleRight != null)
            mTvTitleRight.setOnClickListener(onClickListener);
    }

    public void setMuteAndStrongRemind(boolean showMute, boolean strongRemind) {
        if (!showMute && !strongRemind)
            mIvMute.setVisibility(GONE);
        else {
            mIvMute.setVisibility(VISIBLE);
            mIvMute.setImageResource(strongRemind ? R.drawable.wechat_icon_notice : R.drawable.wechat_icon_mute);
        }
    }
}
